(function(){
var cdn_modernizr = '//uwm-universalmenu.googlecode.com/git/modernizr.min.js';
var cdn_jquery = '//uwm-universalmenu.googlecode.com/git/jquery.min.js';
//var cdn_init = '//uwm-universalmenu.googlecode.com/git/init.js';
var loadjq = function(){ window.Modernizr.load({load:[cdn_jquery],complete:function(){init()}})};
var script_tag = document.createElement('script');
	script_tag.setAttribute('src',cdn_modernizr);
	script_tag.onload = loadjq;
	script_tag.onreadystatechange = function(){if(this.readyState=='complete'||this.readyState=='loaded')loadjq()};
document.getElementsByTagName('head')[0].appendChild(script_tag);
function init(){
	window.requestAnimFrame = (function(){return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||function(callback){window.setTimeout(callback,1000/60)}})();
	(function($){$(function(){
		var app = {};
		app.win = $(window);
		app.doc = $(document);
	    app.doc.find('.uwm-universal-menu').remove();
	    app.um = $('<div class="uwm-universal-menu"><iframe></iframe></div>');
		app.doc.find('body').append( app.um );
		app.umdoc = app.um.find('iframe').contents().find('body').parents('html');

		//app.umidoc = app.um.find('iframe').document;
		//app.umidoc.open();
		//app.umidoc.close();
		//Set innerHTML of the body tag.
		//doc.body.innerHTML = '<p>NETSCAPE</p>';

		app.umbody = app.umdoc.find('body');
		app.umhead = app.umdoc.find('head');

		app.cdn_bootstrap_css = '//uwm-universalmenu.googlecode.com/git/bootstrap.min.css';
		app.cdn_fontawesome_css = '//uwm-universalmenu.googlecode.com/git/font-awesome.min.css';
		app.cdn_app_css = '//uwm-universalmenu.googlecode.com/git/app.css';
		app.umhead.html('<link rel="stylesheet" href="'+app.cdn_bootstrap_css+'" /><link rel="stylesheet" href="'+app.cdn_fontawesome_css+'" /><link rel="stylesheet" href="'+app.cdn_app_css+'">');
		//app.umhead.html('<script type="text/javascript" src="'+cdn_modernizr+'" /><script type="text/javascript" src="'+cdn_init+'" />');

		var data = '<div class="app" style="display: none"><div class="takeovers">'+
		'<div class="takeover toggle-actiontoday"></div>'+
		'</div>'+
		'<div class="scrolldownbar">'+
		'<div class="logo">'+
		'<a target="_parent" href="//uwm.edu">'+
		'<img src="//awesomesupercool.aws.af.cm/apps/uwm/may2014/uwm-sq-full.svg" alt="University of Wisconsin - Milwaukee">'+
		'</a>'+
		'</div>'+
		'<div class="today">Today <span class="label label-warning">10</span></div>'+
		'<div class="apps dropdown">'+
		'<span class="action action-login" data-toggle="toggle-actionlogin">Login <span class="fa fa-caret-down"></span> </span>'+
		'</div>'+
		'<div class="nav-search">'+
		'<form action="//www4.uwm.edu/search.cfm" method="get">'+
		'<span class="fa fa-search"></span>'+
		'<input type="text" name="q" value="Search">'+
		'</form>'+
		'</div>'+
		'</div>'+
		'<div class="nav-search-results">'+
		'<div class="nav-search-loading">Loading more...</div>'+
		'<div class="nav-search-results-list"></div>'+
		'<div class="nav-search-loadmore"></div>'+
		'<div class="nav-people-results nav-results">'+
		'<div class="nav-people-results-header nav-results-header"></div>'+
		'<div class="nav-people-results-list nav-results-list"></div>'+
		'</div>'+
		'<div class="nav-whitepages-results nav-results">'+
		'<div class="nav-whitepages-results-header nav-results-header"></div>'+
		'<div class="nav-whitepages-results-list nav-results-list"></div>'+
		'</div>'+
		'<div class="nav-web-results nav-results">'+
		'<div class="nav-web-results-header nav-results-header"></div>'+
		'<div class="nav-web-results-list nav-results-list"></div>'+
		'</div>'+
		'</div>'+
		'</div>';

		app.umbody.html( $(data) );
		
		app.doc.find('div.uwm-universal-menu').css({'position':'fixed','left':'0px','right':'0px','top':'0px','height':'51px','overflow':'hidden','z-index':'9999' });
		app.um.find('iframe').css({'width':'100%','height':'100%','border':'0px','overflow':'hidden' });
		
		app.umbody.find('[class*=toggle-]').hide();
		app.umbody.find('[data-toggle]').css({'cursor':'pointer'});
		app.umbody.find('[data-toggle]').click(function(e){
			e.preventDefault();
			var el = $(this);
			//if( e.find('.btn').length > 0 ) e.hide();
			//app.umbody.find('[class*=toggle-]').hide();
			if( app.state == 'off' ) {
				app.toggleFocus('on');
				app.umbody.find('.'+ el.attr('data-toggle') ).show();
			} else {
				if( app.umbody.find('.'+ el.attr('data-toggle')+':visible' ).length > 0 ){
					app.toggleFocus('off');
				} else {
					app.umbody.find('.'+ el.attr('data-toggle')+':visible' ).hide();
					app.umbody.find('.'+ el.attr('data-toggle') ).show();
				}
			}

		});

		app.umbody.css({'overflow':'hidden'});

		app.umbody.find('.dropdown-toggle').click(function(e){
			e.preventDefault();
			var el = $(this);
			if( app.state == 'off' ) {
				el.parents('.dropdown').find('.dropdown-menu').show();
				app.toggleFocus('on');
			} else {
				el.parents('.dropdown').find('.dropdown-menu').hide();
				app.toggleFocus('off');
			}
		});

		app.st = 0;
		//console.log(app.bt);


		app.looper = function(){
			window.requestAnimFrame(app.looper);
			var st = app.doc.scrollTop();
			var direction = st - app.st;
			if( direction > 0 && st > 50 ){ // moving down
				app.um.not('.outofview').not(':animated').animate({ 'top': -50},150).addClass('outofview').removeClass('inview');
			} else if ( direction < 0 || st <= 0 ) {
				app.um.not('.inview').not(':animated').animate({ 'top': 0},150).removeClass('outofview').addClass('inview');
			}
			app.st = st;
		}

		app.looper();

		/* Other page manip stuff */
		app.doc.find('body').css({'margin-top':'50px','position':'relative'});
		app.doc.find('.web-feedback').hide();
		app.doc.find('.menu-more-toggle').hide();
		app.doc.find('.search-toggle').hide();
		app.doc.find('.domain-banner').hide();

		app.apiurl = '//www4.uwm.edu/webassets/webid/v1/index.cfm/';

		app.defaultsearch = "Search UWM";


		app.search = {};


		app.umbody.find('.nav-search input[name=q]').val(app.defaultsearch);


		app.umbody.find('.nav-search input[name=q]').keyup(function(e) {
			if( e.keyCode == 13 ) return;
			var field = $(this);
			//var list = $('.nav-search-results-list');
			clearTimeout($.data(this, 'timer'));
			var wait = setTimeout(function(){
				var q = field.val().trim();
				if( q != app.defaultsearch && q.length >= 2 ){
					app.search.searchQuick(q);
					app.toggleFocus('on');
					//app.umbody.find('.nav-results-list').html('');
					app.umbody.find('.nav-people-results:visible').hide();
					app.umbody.find('.nav-whitepages-results:visible').hide();
					app.umbody.find('.nav-web-results:visible').hide();
					//app.umbody.find('.nav-search-results-list:visible').hide();
					app.umbody.find('.nav-search-loadmore').show().html( '<span class="fa fa-long-arrow-right"></span> Show more matches for <strong>&ldquo;'+q+'&rdquo;</strong>');
					app.umbody.find('.nav-search-results').show();
					//app.umbody.find('.nav-search-loading').show();
					app.umbody.find('.remove_filter').show();
				} else {
					app.toggleFocus('off');
					//app.umbody.find('.remove_filter').hide();
					//app.umbody.find('.nav-search-results').hide();
					//field.focus();
				}
				//app.search.searchAll(field.val());
			}, 100);
			$(this).data('timer', wait);
		});

		app.umbody.find('.nav-search input[name=q]').focus(function(){
			//app.doc.find('html').addClass('searchopen');
			var field = $(this);
			//app.umbody.find('.header-navicon').trigger('toggle');				
			if( field.val() == app.defaultsearch ) field.css({'color': '#000000'}).val('');
			field.trigger('keyup');
			//app.toggleFocus('on');
		});
		
		app.umbody.find('.nav-search input[name=q]').blur(function(){
			var field = $(this);
			//app.toggleFocus('off');
			//app.doc.find('html').removeClass('searchopen');
			//if( field.val() == '' )
				//field.css({'color': '#666666'}).val(app.defaultsearch);
		});
		
		app.umbody.find('.nav-search form').submit(function(e){
			e.preventDefault();
			var field = $(this).find('input[name=q]');
			if( field.val() == app.defaultsearch || field.val() == '' ){
				field.focus();
				return;
			}
			//field.trigger('blur');
			app.umbody.find('.nav-results-header').hide('').html('');
			if( app.umbody.find('.nav-search-results-list li').length == 0 ){
				app.umbody.find('.nav-search-loadmore').trigger('click');
			}
		});



		app.umbody.find('.nav-search-loadmore').click(function() { 
			var field = app.umbody.find('.nav-search input[name=q]');
			app.search.searchAll(field.val());
			app.umbody.find('.nav-results-list').show();

		});

		app.umbody.find('.remove_filter').click(function() { 
			$(this).hide();
			var field = app.umbody.find('.nav-search input[name=q]');
			app.doc.find('html').removeClass('searchopen');
			//if( field.val() == '' ) field.css({'color': '#666666'}).val(app.defaultsearch);
			field.val('').trigger('keyup');
		});

		app.umbody.find('.nav-results-header').click(function(){
			var results = $(this).parents('.nav-results:not(:has(.no-results))');
			results.find('.nav-results-list').show();
		});


		app.search.searchAll = function(q){
			//app.umbody.find('.nav-search-loading').show();
			app.umbody.find('.nav-search-loadmore').html('Loading...');
			app.search.searchQuick(q);
			$.getJSON(app.apiurl + 'apps/search?w=people&q=' + q + '&format=jsonp&callback=?', function(data){
				app.search.showPeople(q,data.RESULT.PEOPLE);
				app.search.showWhitepages(q,data.RESULT.DEPTS);
				app.search.showWeb(q,data.RESULT.WEB);
				app.umbody.find('.nav-search-loadmore').hide();
			});
		}

		app.preg_quote = function(str) {
		    return (str+'').replace(/([\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:])/g, "\\$1");
		}
		app.highlight = function(data,search){
		    return data.replace( new RegExp( "(" + app.preg_quote( search ) + ")" , 'gi' ), "<strong>$1</strong>" );
		}

		app.search.searchQuick = function(q){
			$.ajax({
				url: '//awesomesupercool.aws.af.cm/uwm/uwmrsp/quicksearch.php?q='+q,
				type: 'GET',
				success: function(data){
					//app.umbody.find('.nav-search-loading').hide();
					app.umbody.find('.nav-search-results-list').show().html('<ul></ul>');
					var c = 0;
					for(c in data){
						var tag = '';
						var desc = '';
						var moreurls = '';
						var i = 0;
						
						/*
						if( data[c].url && data[c].url.trim() != '' ) {
							if( app.umbody.find('.quickitem-tag a[href="'+data[c].url+'"]').length > 0 ) { 
								continue;
							}
						}
						*/

						if( data[c].tags && data[c].tags[0] ) {
							tag = app.highlight( data[c].tags[0], q);
						}

						if( data[c].description ) {
						//for(i in data[c].description)
							desc = '<div class="quickitem-desc">' + app.highlight(data[c].description,q) + '</div>';
						}

						if( data[c].urls ) {
							var urls = data[c].urls;
							moreurls = '<div class="quickitem-moreurls"><ul>';

							for(i in urls) {
								var url = urls[i];
								if( url.url && url.title )
									moreurls = moreurls + '<li><a target="_parent" href="'+url.url+'">' + app.highlight(url.title,q) + '</a></li>';
							}

							moreurls = moreurls + '</ul></div>';

						}
						//console.log(moreurls);

						if(tag !='')
							app.umbody.find('.nav-search-results-list ul').first().append('<li>'+'<div class="quickitem-title"><a target="_parent" href="'+data[c].url+'">' +tag + '</a></div>'+ desc + moreurls +'</li>' );
						c++;
					}
					if(c==0)
						app.umbody.find('.nav-search-results-list').hide();
				}
			});

		}


		app.search.showPeople = function(q,data){
			//var q = app.umbody.find('.nav-search input[name=q]').val();
			var el = app.umbody.find('.nav-people-results-list');
			var limit = 999;
			el.html('').hide().parent().show();
			if( Object.prototype.toString.call( data ) === '[object Array]' ){
				var plural = '';
				if( data.length > 1) plural = 'es';
				if( data.length == 1) el.show();
				el.siblings('.nav-results-header').show().html('<span class="fa fa-user"></span> ' +  data.length.toString() + ' people match'+plural+' for <strong>' + q + '</strong>');
				for ( i in data) {
					if( i < limit){
						var item = data[i];
						//console.log(item);
						el.append(
							'<div class="result">' +
								'<div class="result-name">' + item.cn + '</div>' +
								'<div class="result-email">' + item.mail + '</div>'+
								'<div class="result-phone">' + item.telephonenumber + '</div>'+
								'<div class="result-title">' + item.appointingdepartment + ' ' + item.title + ' ' + item.classification + '</div>' +
								'<div class="result-loc">' + item.buildingname + ' ' + item.roomnumber + '</div>'+
							'</div>'
						);
					}
				}
			} else {
				el.siblings('.nav-results-header').show().html(
					'<div class="no-results"><span class="fa fa-user"></span> No people matches for <strong>' + q + '</strong></div>'
				);
			}

		} // People Click



		app.search.showWhitepages = function(q,data){
			//var q = app.umbody.find('.nav-search input[name=q]').val();
			var el = app.umbody.find('.nav-whitepages-results-list');
			var limit = 5;
			el.html('').hide().parent().show();
			//console.log(data);
			if( Object.prototype.toString.call( data.RESULTS ) !== '[object Array]' ){
				el.siblings('.nav-results-header').show().html(
					'<div class="no-results"><span class="fa fa-book"></span> No directory matches for <strong>' + q + '</strong></div>'
				);
			} else {
				var plural = '';
				if( data.RESULTS.length > 1) plural = 'es';
				el.siblings('.nav-results-header').show().html('<span class="fa fa-book"></span> ' + data.RESULTS.length.toString() + ' directory match'+plural+' for <strong>' + q + '</strong>');
				//console.log(data.RESULTS);
				for ( i in data.RESULTS) {
					if( i < limit){
						var item = data.RESULTS[i];
						/*
						var urls = '<div class="result-details"><pre>'+item.DETAILS_PLAIN+'</pre></div>';
						for ( c in item.URLS) {
							urls = urls + '<div class="result-url"><a target="_parent" href="' + item.URLS[c] + '">' + item.URLS[c] + '</a></div>';
						}
						*/
						el.append(
							'<div class="result">' +
								'<div class="result-details"><pre>'+item.DETAILS_PLAIN+'</pre></div>'+
								//'<div class="result-title">' + item.TITLE + '</div>'+
								//'<div class="result-urls">' + urls + '</div>'+
							'</div>'
						);
					}
				}
			}
		}// Whitepages Click


		app.search.showWeb = function(q,data){
			//var q = app.umbody.find('.nav-search input[name=q]').val();
			var el = app.umbody.find('.nav-web-results-list');
			var limit = 3;
			el.html('').parent().show();
			//console.log(data);
			if( typeof data.RESULTS.RES == 'undefined' ){
				el.siblings('.nav-results-header').show().html(
					'<div class="no-results"><span class="fa fa-file"></span> No web matches for <strong>' + q + '</strong></div>'
				);
			} else {
				if( Object.prototype.toString.call( data.RESULTS.RES.R ) === '[object Object]' ) {
					el.siblings('.nav-results-header').show().html('<span class="fa fa-file"></span> 1 web match for <strong>' + q + '</strong>');
					var item = data.RESULTS.RES.R;
					if(item.T ){ 
						var durl = item.U.replace('http://www4.uwm.edu/','/');
						el.append(
							'<div class="result">' +
								'<div class="result-title"><a target="_parent" href="'  + item.U + '">' + item.T + '</a></div>'+
								'<div class="result-desc">' + item.S + '</div>'+
								'<div class="result-url"><a target="_parent" href="'  + item.U + '">'  + durl + '</a></div>' +
							'</div>'
						);
					}
				} else if( Object.prototype.toString.call( data.RESULTS.RES.R ) === '[object Array]' ){
					el.siblings('.nav-results-header').show().html('<span class="fa fa-file"></span> ' + data.RESULTS.RES.M + ' web matches for <strong>' + q + '</strong>');
					//console.log( data.RESULTS );
					for ( i in data.RESULTS.RES.R) {
						if( i < limit){
							var item = data.RESULTS.RES.R[i];
							if(item.T ){ 
								var durl = item.U.replace('http://www4.uwm.edu/','/');
								el.append(
									'<div class="result">' +
										'<div class="result-title"><a target="_parent" href="'  + item.U + '">' + item.T + '</a></div>'+
										'<div class="result-desc">' + item.S + '</div>'+
										'<div class="result-url"><a target="_parent" href="'  + item.U + '">'  + durl + '</a></div>' +
									'</div>'
								);
							}
						}
					}
					el.append( '<a class="btn btn-warning btn-lg btn-block" target="_parent" href="http://www4.uwm.edu/search.cfm?q='+q+'&s=people-depts-web">View all ' + data.RESULTS.RES.M + ' web matches <span class="fa fa-long-arrow-right"></span></a>');

				}

			}

		}// Web Click


		//}

		//app.search.setupSearch();
		app.state = 'off';

		app.toggleFocus = function(state){
			if(state == 'on'){
				app.state = 'on';
				//var h = app.umbody.contents().outerHeight();
				app.um.css({'height': app.win.height()+'px'});
				//app.umbody.css({'overflow-y':'scroll'});
				app.umbody.addClass('focused');
				app.doc.find('html').css({'overflow':'hidden'});
			} else if( state == 'off'){
				app.state = 'off';
				app.umbody.find('[class*=toggle-]').hide();
				app.um.css({'height': '51px'});
				//app.umbody.css({'overflow':'hidden'});
				app.umbody.find('.remove_filter').hide();
				app.umbody.find('.nav-search-results').hide();
				app.umbody.find('.dropdown').find('.dropdown-menu').hide();
				app.umbody.removeClass('focused');
				app.doc.find('html').css({'overflow':'auto'});
			}

		}

		app.umbody.click(function(event) {
		    if ( !$(event.target).closest( '.takeover, .scrolldownbar, .nav-search, .nav-search-results').length ) {
				app.toggleFocus('off');
		    };
		});

		
	//}
})})(jQuery)}
})()